import { saveLink } from "@/api/link.api";
import { Link } from "@/types/link";
import { reactive, toRaw } from "vue";

export const useSaveLink = (): {
  linkForm: Link;
  save: () => void;
} => {
  const linkForm = reactive<Link>({
    id: "",
    name: "",
    logo: "",
    description: "",
    address: "",
    status: "2",
  });
  const save = async () => {
    if (!(linkForm.name && linkForm.address && linkForm.logo)) return;
    const res = await saveLink(toRaw(linkForm));
    if (res.code === 200) {
      linkForm.address = "";
      linkForm.logo = "";
      linkForm.description = "";
      linkForm.name = "";
    }
  };
  return {
    linkForm,
    save,
  };
};
